package com.ysxsoft.wdg.ui.activity.ht;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.classic.common.MultipleStatusView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.adapter.ht.HtAdapter;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.response.CollectShopResponse;
import com.ysxsoft.wdg.bean.response.CouponListResponse;
import com.ysxsoft.wdg.bean.response.HuiTaoResponse;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.net.UrlHost;
import com.ysxsoft.wdg.ui.activity.KanWebViewActivity;
import com.ysxsoft.wdg.util.JsonUtils;
import com.ysxsoft.wdg.util.ListObserverMap;
import com.ysxsoft.wdg.util.ToastUtils;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;
import com.ysxsoft.wdg.util.transform.GlideCircleTransform;
import com.ysxsoft.wdg.util.transform.GlideRoundTransform;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;


public class MyHtActivity extends BaseActivity implements OnLoadmoreListener, OnRefreshListener, HtAdapter.OnChangeListener {


    @BindView(R.id.iv_top)
    ImageView ivTop;
    @BindView(R.id.iv_tx)
    ImageView ivTx;
    @BindView(R.id.tv_nick)
    TextView tvNick;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.multStatusView)
    MultipleStatusView multStatusView;

    private HtAdapter adapter;
    private String name, icon;
    private int page = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_ht;
    }

    @Override
    protected void initData() {
        name = getIntent().getStringExtra("name");
        icon = getIntent().getStringExtra("icon");
        adapter = new HtAdapter(R.layout.item_ht);
        adapter.setListener(this);
        adapter.setType(0);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                HuiTaoResponse.DataBeanX.DataBean d = (HuiTaoResponse.DataBeanX.DataBean) adapter.getItem(position);
//                String url = "http://zhwdg.sanzhima.cn/index.html#/particulars?no=" + d.getId() + "&iszan=" + d.getIszan() + "&type=1";  //type1是android 回调back方法
                String url = UrlHost.BASE_URL+"/dist/index.html#/particulars?no=" + d.getId() + "&iszan=" + d.getIszan() + "&type=1";  //type1是android 回调back方法
//                String url = "http://www.baidu.com";
                Log.e("tag", "url:" + url);
                KanWebViewActivity.start(MyHtActivity.this, url);
            }
        });
        recyclerview.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        recyclerview.setAdapter(adapter);
        tvNick.setText(name + "");
        Glide.with(MyHtActivity.this).load(icon).into(ivTop);

        getList();
    }

    @Override
    protected void setListener() {
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setOnLoadmoreListener(this);
        refreshLayout.setOnRefreshListener(this);
    }

    @OnClick(R.id.iv_finish)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        page++;
        getList();
    }

    private boolean isFrist = false;

    private void getList() {
        if (!isFrist) {
            isFrist = true;
            multStatusView.showLoading();
        }
        OkHttpUtils.post()
                .url(UrlHost.MY_HUI)
                .addParams("uid", SharePrefUtils.getUserId())
                .addParams("page", page + "")
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (!call.isCanceled()) {
                    reset();
                    multStatusView.showContent();
                }
            }

            @Override
            public void onResponse(String response, int id) {
                reset();
                if (response != null) {
                    multStatusView.showContent();
                    HuiTaoResponse messageBean = JsonUtils.parseByGson(response, HuiTaoResponse.class);
                    if (messageBean == null) {
                        return;
                    }
                    if (CallbackCode.SUCCESS.equals(messageBean.getCode()) && messageBean.getData() != null) {
                        List<HuiTaoResponse.DataBeanX.DataBean> list = messageBean.getData().getData();
                        if (page == 1) {
                            adapter.setNewData(list);
                        } else {
                            adapter.addData(list);
                        }
                        if (messageBean.getData() != null) {
                            HuiTaoResponse.DataBeanX.UserBean bean = messageBean.getData().getUser();
                            if (bean != null) {
                                String avatar = bean.getAvatar();
                                Glide.with(MyHtActivity.this).load(avatar).apply(new RequestOptions().transform(new GlideCircleTransform())).into(ivTx);
                            }
                        }
                    } else if (CallbackCode.EMPTY.equals(messageBean.getCode())) {
                        multStatusView.showEmpty();
                    }
                } else {
                    multStatusView.showError();
                }
            }
        });
    }

    private void reset() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.finishRefresh(true);
        }
        refreshLayout.finishLoadmore();
    }


    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        page = 1;
        getList();
    }

    @Override
    public void onDelete(String hid) {
        delete(hid);
    }

    /**
     * 取消订单
     */
    private void delete(String hid) {
        OkHttpUtils.post()
                .url(UrlHost.DELETE_HUI_TAO)
                .addParams("uid", SharePrefUtils.getUserId())
                .addParams("hid", hid)//订单id
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (!call.isCanceled()) {
                }
            }

            @Override
            public void onResponse(String response, int id) {
                if (response != null) {
                    CollectShopResponse collectShopResponse = JsonUtils.parseByGson(response, CollectShopResponse.class);
                    if (collectShopResponse == null) {
                        return;
                    }
                    if (CallbackCode.SUCCESS.equals(collectShopResponse.getCode())) {
                        page = 1;
                        getList();
                    } else {
                        ToastUtils.showToast(collectShopResponse.getMs());
                    }
                } else {
                }
            }
        });
    }


}
